In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import plotly.express as px
import pymc3
import plotly.graph_objects as go
import plotly.figure_factory as ff
In [2]:
size=20000
#facebook
facebook_budget=15
ads_days=5
conversion=[17,48]
reach=[527,1500]

#ecommerce
ali_price=6.19
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*1.5
#selling_price=19

#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35

#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)

    
for i in range(size):
    
    prior=np.random.beta(np.random.randint(conversion[0],conversion[1]),(np.random.randint(reach[0],reach[1])-np.random.randint(conversion[0],conversion[1])),size=ads_days)
    likelihood=np.random.binomial(np.random.randint(reach[0],reach[1]),prior,size=ads_days)
    
    prior_arr[i]=round(np.mean(prior)*100,2)
    likelihood_arr[i]=np.sum(likelihood)
    
df=pd.DataFrame(data={"Conversion rate":prior_arr,"Conversions":likelihood_arr})

df['Revenue']=df["Conversions"]*selling_price
df['Cost of Sales']=df["Conversions"]*(ali_price+shipping)
df['Gross profit']=df['Revenue']-df['Cost of Sales']
df["Expences"]=ads_days*facebook_budget+20+12
df['Operation Profit']=df['Gross profit']-df["Expences"]
df['Tax']=df['Operation Profit']*0.2
df['Net Profit']=df['Operation Profit']-df['Tax']

df['Investment Nedded']=df['Cost of Sales']+df["Expences"]
df["Profit per unit"]=round(df["Net Profit"]/df['Conversions'],2)
df['Return on investment']=round(df['Net Profit']/df['Investment Nedded']*100)

print("Selling price is:",selling_price)
for i in ["Net Profit","Investment Nedded",'Return on investment',"Conversion rate","Conversions",'Profit per unit']:
    fig1=px.histogram(df,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i == "Conversion rate":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0],2),"and",round(pymc3.stats.hpd(df[i])[1],2))
    else:
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]),"and",round(pymc3.stats.hpd(df[i])[1]))
    fig1.show()
    print("________________________________________________")
Selling price is: 15.475000000000001
The HPD of the Net Profit is bettwen 204 and 2484
________________________________________________
The HPD of the Investment Nedded is bettwen 367 and 2267
________________________________________________
The HPD of the Return on investment is bettwen 76 and 112
________________________________________________
The HPD of the Conversion rate is bettwen 1.15 and 6.18
________________________________________________
The HPD of the Conversions is bettwen 39 and 346
________________________________________________
The HPD of the Profit per unit is bettwen 6 and 7
________________________________________________
In [ ]:
 
In [3]:
df.head()
Out[3]:
Conversion rate Conversions Revenue Cost of Sales Gross profit Expences Operation Profit Tax Net Profit Investment Nedded Profit per unit Return on investment
0 1.74 109.0 1686.775 674.71 1012.065 107 905.065 181.013 724.052 781.71 6.64 93.0
1 4.90 291.0 4503.225 1801.29 2701.935 107 2594.935 518.987 2075.948 1908.29 7.13 109.0
2 8.47 413.0 6391.175 2556.47 3834.705 107 3727.705 745.541 2982.164 2663.47 7.22 112.0
3 2.25 76.0 1176.100 470.44 705.660 107 598.660 119.732 478.928 577.44 6.30 83.0
4 5.37 249.0 3853.275 1541.31 2311.965 107 2204.965 440.993 1763.972 1648.31 7.08 107.0
In [ ]:
 
In [4]:
df2=df
df2=df2[df2['Conversion rate']<=2]
#df2=df2[df2['Investment Nedded']<=500]

print("Selling price is:",selling_price)
for i in ["Net Profit","Investment Nedded",'Return on investment',"Conversion rate","Conversions",'Profit per unit']:
    fig1=px.histogram(df2,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df2[i])[0], pymc3.stats.hpd(df2[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df2[i])[1], pymc3.stats.hpd(df2[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i == "Conversion rate":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df2[i])[0],2),"and",round(pymc3.stats.hpd(df2[i])[1],2))
    else:
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df2[i])[0]),"and",round(pymc3.stats.hpd(df2[i])[1]))
    fig1.show()
    print("________________________________________________")
Selling price is: 15.475000000000001
The HPD of the Net Profit is bettwen 182 and 902
________________________________________________
The HPD of the Investment Nedded is bettwen 330 and 930
________________________________________________
The HPD of the Return on investment is bettwen 64 and 99
________________________________________________
The HPD of the Conversion rate is bettwen 1.22 and 2.0
________________________________________________
The HPD of the Conversions is bettwen 36 and 133
________________________________________________
The HPD of the Profit per unit is bettwen 6 and 7
________________________________________________
In [ ]:
 
In [ ]: